/*$(document).ready(function() {*/
  	var current_selector_dom_obj = null;
  	var current_selector = null;
  	var current_operate = null;
  	
  	var post_url = null;
  	var post_option = { };
  	var res = '';
  	
  	function bootstrap_contextmenu() {
  		switch (controller) {
  		<?php foreach ($type as $v):?>
  		case '<?php echo $v['variable']?>':
  			switch (type) {
			<?php foreach ($v['child'] as $v1):?>
  			case '<?php echo $v1['variable']?>':
  				<?php if (!empty($v1['editable_obj'])) {
  					$temp1 = unserialize($v1['editable_obj']);
  					foreach ($temp1 as $v) {
  						$temp2[] = $v['selector'];
  					}
  					$temp_str = implode(', ', $temp2);
  					unset($temp2);
  				}?>
				editable_obj = <?php echo "'" . (isset($temp_str) ? $temp_str : '') . "'"; unset($temp_str);?>;
				hover(editable_obj);
				
				var option = <?php echo json_encode_jsfunc($option, array('action', 'onShow'))?>;
				
				$(editable_obj).contextmenu(option);
  				break;
			<?php endforeach;?>
  			}
			break;
		<?php endforeach;?>
  		}
  	}
  	
  	function hover(obj) {
  		$(obj).live('mouseover', function(event) {
  			$(this).addClass('edit-outline');
  		}).live('mouseout', function(event) {
  			$(this).removeClass('edit-outline');
  		});
  	}
  	
  	function menuAction() {
  		var edit_obj_array = editable_obj.split(', ');
  		
  		for (x in edit_obj_array) {
  			if (current_selector_dom_obj == $(edit_obj_array[x])[0]) {
  				current_selector = edit_obj_array[x];
  			}
  		}
  		current_operate = this.data.alias;
  		
  		set_ajax_post_attributes(false);
  		
  		$.post(post_url, post_option,
		function (html) {
  			$("#operation_modal").html(html);
  			$("#operation_modal").modal({ /*backdrop: 'static', */keyboard: false }, 'show');
  		}, "html");
  	}
  	function applyrule(menu) {
  		switch (controller) {
  		<?php foreach ($type as $v):?>
  		case '<?php echo $v['variable']?>':
  			switch (type) {
			<?php foreach ($v['child'] as $v1):?>
  			case '<?php echo $v1['variable']?>':
  				<?php if (!empty($v1['editable_obj'])) {
  					$temp1 = unserialize($v1['editable_obj']);
  				}?>
  				<?php if (!empty($v1['editable_obj'])):?>
  				switch (this) {
  				<?php foreach ($temp1 as $v2):?>
  		  		case $('<?php echo $v2['selector']?>')[0]:
  		  			<?php
  		  			$disable_items = array();
  		  			foreach ($v2['permission'] as $k3 => $v3) {
  		  				if ($v3 == false) {
  		  					$disable_items[] = $k3;
  		  				}
  		  			}
  		  			?>
  		  			current_selector_dom_obj = this;
  		  			menu.applyrule(<?php echo json_encode(array('name' => str_replace(' ', '_', trim($v2['selector'], '#.')), 'disable' => true, 'items' => $disable_items));?>);
  		  			break;
  		  		<?php endforeach;?>
  		  		}
  		  		<?php endif;?>
  				break;
			<?php endforeach;?>
  			}
			break;
		<?php endforeach;?>
  		}
  		  		
  		$(".edit-target").removeClass('edit-target');
  		$(".edit-outline").addClass('edit-target');
  	}
  	$(document).live('click', function() {
		$(".edit-target").removeClass('edit-target');
	});
  	
  	$("#operation_modal.modal > .modal-footer > .btn.edit-submit").live('click', function() {
  		set_ajax_post_attributes(true);
  		do_ajax_post();
  	});
  	
  	function set_ajax_post_attributes(is_submit) {
  		post_url = null;
  		//post_option = { };
  		
  		post_option['is_submit'] = (is_submit == true) ? true : false;
  		
  		if (current_operate == 'modify_pwd' || current_operate == 'logout') {
  			post_url = base_url + "index.php/backend/admin/" + current_operate;
  		} else {
  			post_url = base_url + "index.php/backend/" + controller + "/common";
  			
  			post_option['type'] = type;
  			post_option['selector'] = selector_replace(current_selector);
  			post_option['operate'] = current_operate;
  			
  			if (controller == 'about_us' && current_selector == '.flip-block') {
  				post_option['content_id'] = $("div.flip-block.edit-target").attr('id');
  			}
  			if (controller == 'project' && current_selector == '#project_carousel .viewport li') {
  				post_option['project_id'] = $("li.edit-target").attr('id');
  			}
  			if (controller == 'media') {
  				switch (current_selector) {
  					case '#media_carousel .viewport .news > img':
  					case '#media_carousel .viewport .news > div':
  						post_option['media_id'] = $("#media_carousel div.news > .edit-target").parent("div.news").attr('id');
  						break;
  				}
  			}
  			if (controller == 'academy') {
				switch (current_selector) {
  					case '.inner-content .courses':
  						post_option['course_id'] = $("div.courses.edit-target span").attr('id');
  						break;
  					case '.inner-content .exchanges':
  						post_option['exchange_id'] = $("div.exchanges.edit-target span").attr('id');
  						break;
  				}
  			}
  			if (controller == 'join_us') {
				switch (current_selector) {
  					case '.inner-content .activities':
  						post_option['activity_id'] = $("div.activities.edit-target span").attr('id');
  						break;
  				}
  			}
  			
  			if (is_submit == true) {
	  			if ($("#operation_modal.modal > .modal-body form textarea").length == 1) {
	  				editor_id = $("#operation_modal.modal > .modal-body form textarea").attr('id');
	  	  	  		editor_data = CKEDITOR.instances[editor_id].getData();
	  	  	  		post_option['form_textarea_data'] = editor_data;
	  			}
	  			post_option['form_fields_data'] = $("#operation_modal.modal > .modal-body form").serializeArray();
  			}
  		}
  	}
  	
  	function reload_page() {
  		if (current_operate == 'logout') {
			$(".btn.edit-submit").button('complete');
			setTimeout('location.reload()', 3000);
		} else {
			$(".btn.edit-submit").button('reset');
			
			var hash_piece = window.location.hash.split('&');
			if (hash_piece[1] != undefined) {
				if (hash_piece[1] == 'sub_page') {
					if (hash_piece[0] == '#design_team') {
						var param = { };
						param['id'] = hash_piece[2];
						do_ajax_post_by_condition(1, param);
					} else if (hash_piece[0] == '#events') {
						var param = { };
						param['year'] = hash_piece[2];
						do_ajax_post_by_condition(2, param);
					} else if (hash_piece[0] == '#manager_community') {
						var param = { };
						do_ajax_post_by_condition(4, param);
					}
				} else if (hash_piece[1] == 'flip_sub_page') {
					var param = { };
					param['request'] = hash_piece[1];
					param['sub_page_type'] = hash_piece[2];
					do_ajax_post_by_condition(5, param);
				}
			} else {
				$("#menu .child ul li.active").trigger('click');
			}
		}
  	}
  	
  	function do_ajax_post() {
  		var inject_exec = arguments[0] ? arguments[0] : '';
  		
  		$.post(post_url, post_option,
		function (data) {
  			if (data == 'success' || data != '') {
  				reload_page();
  				
  				if (data != '' && inject_exec != '') {
  					eval(inject_exec);
  				}
  			}
  		}, 'json');
  	}
/*});*/